package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.dto.LoginDto;
import com.heima.media.dto.WmNewsDto;
import com.heima.media.entity.WmNews;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-11-10
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    /**
     * 自媒体登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(LoginDto dto) {
        //判断参数是否为空
        if(dto==null || dto.getName()==null || dto.getPassword()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否存在
        LambdaQueryWrapper<WmUser> query=new LambdaQueryWrapper<>();
        query.eq(WmUser::getName,dto.getName());
        WmUser one = this.getOne(query);
        if(one==null){
            //不存在则提示
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取盐
        String salt = one.getSalt();
        //进行加密
        String digest = DigestUtils.md5DigestAsHex((dto.getPassword() + salt).getBytes());
        //进行对比
        if(!digest.equals(one.getPassword())){
            //不一致返回错误提示
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //一致生成token
        HashMap<String, Object> userInfo = new HashMap<>();
        userInfo.put("userId",one.getId());
        userInfo.put("userName",one.getName());
        String token = AppJwtUtil.getToken(userInfo);
        //定义前端返回格式
        HashMap<String, Object> result = new HashMap<>();
        //隐藏敏感信息
        one.setPassword(null);
        one.setSalt(null);
        one.setPhone(null);
        result.put("user",one);
        result.put("token",token);
        return ResponseResult.okResult(result);
    }


    /**
     * 创建自媒体
     * @param user
     * @return
     */
    @Override
    public ResponseResult<WmUser> saveWmUser(WmUser user) {
        //判断参数是否为空,为空则提示
        if(user==null || user.getApUserId()==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断id是否存在
        LambdaQueryWrapper<WmUser> query=new LambdaQueryWrapper<>();
        query.eq(WmUser::getApUserId,user.getApUserId());
        WmUser one = this.getOne(query);
        //存在就是返回自媒体
        if(!StringUtils.isEmpty(one)){
            return ResponseResult.okResult(one);
        }
        //不存在则创建
        user.setCreatedTime(new Date());
        this.save(user);
        return ResponseResult.okResult(user);
    }
}
